package com.tensquare.qa.interceptor;

import io.jsonwebtoken.Claims;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class JwtInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtUtil jwtUtil;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("经过了qa拦截器");
        //无论如何都放行。具体能不能操作还是在具体的操作中去判断。
        //拦截器只是负责把头请求头中包含token的令牌进行一个解析验证。
        String header = request.getHeader("Authorization");
        System.out.println("请求头信息：" + header + "\n判断0：" + header != null && !"".equals(header));
        if (header != null && !"".equals(header)) {
            System.out.println("请求头信息判断1" + header.startsWith("Bearer"));
            //如果有包含有Authorization头信息，就对其进行解析
            if (header.startsWith("Bearer")) {
                System.out.println("请求头信息判断2：" + header.startsWith("Bearer"));
                //得到token
                String token = header.substring(7);
                //对令牌进行验证
                try {
                    System.out.println("try3：" + token);
                    if (!token.equals("") && token != null && !token.equals("undefined")) {
                        Claims claims=null;
                        try {
                           claims = jwtUtil.parseJWT(token);
                        }catch (Exception e){
                            System.err.println("令牌不正确");
                            return true;
                        }

                        String roles = (String) claims.get("roles");
                        System.out.println("用户角色：" + roles);
                        if (roles != null && roles.equals("admin")) {
                            request.setAttribute("claims_admin", token);
                        }
                        if (roles != null && roles.equals("user")) {
                            request.setAttribute("claims_user", token);
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException("令牌不正确！");
                }
            }
        }
        return true;
    }
}
